package com.park.user.anno;

import com.park.jwt.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Administrator
 * @title: TokenAspect
 * @projectName park
 * @description: 用户登录Token校验
 * @date 2020/5/518:11
 */
@Aspect
@Order(1)
@Component
@Slf4j
public class TokenAspect {

    /**
 　　* @description:
 　　* @param [proceedingJoinPoint, userToken]
 　　* @return {@link Object}
 　　* @throws
 　　* @author ZhangHaiWen
 　　* @version 2.0
 　　* @date 2020/5/5 18:20
 　　*/
    @Around(value = "@annotation(userToken)")
    public Object  around(ProceedingJoinPoint proceedingJoinPoint, UserToken userToken) throws Throwable {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String signatureToken = request.getHeader("signatureToken");
        Map<String, String> stringStringMap = TokenUtils.verifyToken(signatureToken);
        String userId = stringStringMap.get("userId");
        request.setAttribute("userId",userId);
        return proceedingJoinPoint.proceed(); //调用目标方法
    }

}
